package com.kye.express.business.order_list.subfragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatFragment;
import com.kye.express.databinding.FragmentOrderListToSignBinding;
import com.kye.lib.view.statuslayout.StatusLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/12/2017
 * @updated : 29/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

@Deprecated
public class OrderListToSignFragment extends BaseAppCompatFragment implements OrderListToSignContract.OrderListToSignView {

    protected FragmentOrderListToSignBinding mDataBinding;
    protected OrderListToSignContract.OrderListToSignPresenter mPresenter;
    protected MultiTypeAdapter mAdapter;

    public static OrderListToSignFragment newInstance() {
        Bundle args = new Bundle();
        OrderListToSignFragment fragment = new OrderListToSignFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list_to_sign, container, false);
        initViews(view, savedInstanceState);
        return view;
    }

    private void initViews(View view, Bundle savedInstanceState) {
        mDataBinding = DataBindingUtil.bind(view);
        mAdapter = new MultiTypeAdapter();
        mDataBinding.orderList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mDataBinding.orderList.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        mDataBinding.orderList.setItemAnimator(new DefaultItemAnimator());
        mDataBinding.orderList.setAdapter(mAdapter);
        mDataBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.loadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.refresh();
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter = new OrderListToSignContract.OrderListToSignPresenter();
        mPresenter.bind(this);
        mPresenter.onCreate(savedInstanceState);
        initEvents(savedInstanceState);
    }

    private void initEvents(Bundle savedInstanceState) {
        RxView.clicks(mDataBinding.llErrorServerCustom.btErrorServerRetry)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mDataBinding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(mDataBinding.llErrorNetCustom.btErrorNetRetry)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mDataBinding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(mDataBinding.llEmptyCustom.llEmpty)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mDataBinding.statusLayout.showLoadingView();
                    }
                });
        mDataBinding.statusLayout.setOnLoadingListener(new StatusLayout.OnLoadingListener() {
            @Override
            public void onLoading() {
                mPresenter.refreshFirstTime();
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void initDataFirstTime(boolean isForceUpdate, @Nullable Bundle savedInstanceState) {
        mDataBinding.statusLayout.showLoadingView();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        mPresenter.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        mPresenter.unbind();
        super.onDestroy();
    }

    @Override
    public void onError() {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded() {

    }

}
